<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wordcloud:main:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wordcloud:main:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wordcloud:main:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wordcloud:main:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="tpId" />
<!--      <el-table-column label="输入文本" align="center" prop="tpContext" />-->
<!--      <el-table-column label="输出结果" align="center" prop="tpResult" />-->
<!--      <el-table-column label="类型" align="center" prop="type" />-->
      <el-table-column label="输入文本" align="center" prop="tpContext" >
        <template slot-scope="scope">
          <el-collapse v-model="activeNames" @change="handleChange">
            <el-collapse-item
              :title="(function() { return scope.row.tpContext ? scope.row.tpContext.slice(0, 15) : '默认标题'; })()"
              :name="`unique-name-${scope.row.tpContext}`">
              <div>{{ scope.row.tpContext }}</div>
            </el-collapse-item>
          </el-collapse>
        </template>
      </el-table-column>
      <el-table-column label="输出结果" align="center" prop="tpResult" >
        <template slot-scope="scope">
          <el-collapse v-model="activeNames" @change="handleChange">
            <el-collapse-item
              :title="(function() { return scope.row.tpResult ? scope.row.tpResult.slice(0, 25) : '默认标题'; })()"
              :name="`unique-name-${scope.row.id}`">
              <div>{{ scope.row.tpResult }}</div>
            </el-collapse-item>
          </el-collapse>
        </template>

      </el-table-column>
      <el-table-column label="图片1" align="center" prop="type" >
        <template slot-scope="scope">
          <!-- <img :src="'http://web.test.net/web/websiteimg/'+scope.row.icon+'.png'" width="80"> -->
          <!--          <img :src="`${scope.row.type}`" width="80">-->
<!--          <img :src="require(`@/assets/wordcloud/${scope.row.type}`)" width="80">-->


          <el-popover placement="left-start" trigger="click">
            <div class="row_reserve" style="height: 600px; width: 600px; border-radius: 60px">
              <img :src = "require(`@/assets/wordcloud/${scope.row.type}`)">
            </div>
            <div slot="reference">
              <img :src = "require(`@/assets/wordcloud/${scope.row.type}`)" width="100" height="100">
            </div>
          </el-popover>

          <!--          <el-image :src="imageUrl" width="80"/>-->
          <!--          <p v-text="scope.row.type"></p>-->
          <!--          <p v-text="imageUrl"></p>-->
          <!--          <p v-text="@/assets/wordcloud/${scope.row.type"></p>-->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="InputToResult(scope.row)"-->
<!--            v-hasPermi="['wordcloud:main:edit']"-->
<!--          >input->result->pic</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wordcloud:main:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wordcloud:main:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改展示词云图对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="输入文本" prop="tpContext">
          <el-input v-model="form.tpContext" type="textarea" placeholder="请输入要分词的内容" />
        </el-form-item>
        <el-form-item label="输出结果" prop="tpResult" hidden="true">
          <el-input v-model="form.tpResult" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="类型" prop="type" hidden="true">
          <el-input v-model="form.type" placeholder="请输入类型" />
        </el-form-item>
<!--        <el-form-item label="图片" prop="image" >-->
<!--          <template>-->
<!--            <img src="../../../assets/wordcloud/whale_wordcloud_small.png" width="100" height="80">-->
<!--          </template>-->
<!--        </el-form-item>-->
        <el-divider content-position="center">词云图从表信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddWordcloudSlave">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteWordcloudSlave">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="wordcloudSlaveList" :row-class-name="rowWordcloudSlaveIndex" @selection-change="handleWordcloudSlaveSelectionChange" ref="wordcloudSlave">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="词云图" prop="tcPic" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.tcPic" placeholder="请输入词云图" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listMain, getMain, delMain, addMain, updateMain, testMain} from "@/api/wordcloud/main";

export default {
  name: "Main",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWordcloudSlave: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 展示词云图表格数据
      mainList: [],
      // 词云图从表表格数据
      wordcloudSlaveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tpContext: null,
        tpResult: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询展示词云图列表 */
    getList() {
      this.loading = true;
      listMain(this.queryParams).then(response => {
        this.mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tpId: null,
        tpContext: null,
        tpResult: null,
        type: null
      };
      this.wordcloudSlaveList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tpId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加展示词云图";
    },
    /** input->result->pic按钮操作 */
    InputToResult(row){
      const tpId = row.tpId || this.ids
      testMain(tpId);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tpId = row.tpId || this.ids
      getMain(tpId).then(response => {
        this.form = response.data;
        this.wordcloudSlaveList = response.data.wordcloudSlaveList;
        this.open = true;
        this.title = "修改展示词云图";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.wordcloudSlaveList = this.wordcloudSlaveList;
          if (this.form.tpId != null) {
            updateMain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMain(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tpIds = row.tpId || this.ids;
      this.$modal.confirm('是否确认删除展示词云图编号为"' + tpIds + '"的数据项？').then(function() {
        return delMain(tpIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 词云图从表序号 */
    rowWordcloudSlaveIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 词云图从表添加按钮操作 */
    handleAddWordcloudSlave() {
      let obj = {};
      obj.tcPic = "";
      this.wordcloudSlaveList.push(obj);
    },
    /** 词云图从表删除按钮操作 */
    handleDeleteWordcloudSlave() {
      if (this.checkedWordcloudSlave.length == 0) {
        this.$modal.msgError("请先选择要删除的词云图从表数据");
      } else {
        const wordcloudSlaveList = this.wordcloudSlaveList;
        const checkedWordcloudSlave = this.checkedWordcloudSlave;
        this.wordcloudSlaveList = wordcloudSlaveList.filter(function(item) {
          return checkedWordcloudSlave.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWordcloudSlaveSelectionChange(selection) {
      this.checkedWordcloudSlave = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wordcloud/main/export', {
        ...this.queryParams
      }, `main_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
